import { handleActions } from "redux-actions";
const initialState = {
  list: [],
  loading: false,
  status: false,
  count: 0,
  page: 1,
  pageSize: 5,
};

export function setListasync(payload) {
  return (dispatch, _, { get }) => {
    dispatch({ type: "set_list_start" });
    return new Promise((reslove, rej) => {
      get("/app/memberAuth", {})
        .then((res) => {
          console.log(res);
          reslove(res);
          dispatch({
            type: "set_list_success",
            payload: { list: res, count: res.length, page: 1 },
          });
        })
        .catch((err) => {
          rej(err);
          dispatch({ type: "set_list_error" });
        });
    });
  };
}
export function setPage(payload) {
  return { type: "set_page", payload };
}
export function delFn(payload) {
  return { type: "del_Fn", payload };
}
// export default (state = initialState, { type, payload }) => {
//   switch (type) {
//     case "set_list_start":
//       return { ...state, loading: true };
//     case "set_list_success":
//       return {
//         ...state,
//         loading: false,
//         list: [...payload.list],
//         count: payload.count,
//         page: payload.page,
//         pageSize: payload.pageSize,
//       };
//     case "set_list_error":
//       return { ...state, loading: false };
//     case "set_page":
//       console.log(11111, payload);
//       return { ...state, page: payload.page, pageSize: payload.pageSize };

//     default:
//       return state;
//   }
// };

export default handleActions(
  {
    ["set_list_start"](state, { type, payload }) {
      return { ...state, loading: true };
    },
    ["set_list_success"](state, { type, payload }) {
      return { ...state, ...payload };
    },
    ["set_list_succeset_list_errorss"](state, { type, payload }) {
      return { ...state, loading: false };
    },
    ["set_page"](state, { type, payload }) {
      return { ...state, ...payload };
    },
    ["set_list"](state, { type, payload }) {
      return { ...state, list: [...payload], count: payload.length };
    },
    ["del_Fn"](state, { type, payload }) {
      let list = [...state.list];
      console.log(list);
      let nlist = [];
      // 批量刪除
      if (Array.isArray(payload)) {
      } else {
        // 单个删除
        nlist = list.filter((v) => {
          return v.id !== payload;
        });

        console.log(nlist);
      }
      return { ...state, list: [...nlist], count: nlist.length };
    },
  },
  initialState
);
